// app/(routes)/legal-works/page.tsx 수정

import * as React from "react";
import { Metadata } from "next";
import { type SearchParams } from "@/types/table";
import { Shell } from "@/components/shell";
import { DataTableSkeleton } from "@/components/data-table/data-table-skeleton";
import { InformationButton } from "@/components/information/information-button";
import { Badge } from "@/components/ui/badge"; // ✅ Badge 추가
import { SearchParamsCacheLegalWorks } from "@/lib/legal-review/validations";
import { getLegalWorks } from "@/lib/legal-review/service";
import { LegalWorksTable } from "@/lib/legal-review/status/legal-table";

export const dynamic = "force-dynamic";
export const revalidate = 0;

export const metadata: Metadata = {
  title: "법무검토 관리",
  description: "법무 검토 요청 및 답변을 관리합니다.",
};

interface LegalWorksPageProps {
  searchParams: Promise<SearchParams>;
}

export default async function LegalWorksPage({ searchParams }: LegalWorksPageProps) {
  const rawParams = await searchParams;
  const parsedSearch = SearchParamsCacheLegalWorks.parse(rawParams);
  
  // ✅ EvaluationTargetsPage와 동일한 패턴으로 currentYear 추가
  const currentYear = new Date().getFullYear();

  const promises = Promise.all([
    getLegalWorks(parsedSearch)
  ]);

  return (
    <Shell className="gap-4">
      {/* Header - EvaluationTargetsPage와 동일한 패턴 */}
      <div className="flex items-center justify-between space-y-2">
        <div className="flex items-center gap-2">
          <h2 className="text-2xl font-bold tracking-tight">법무검토 관리</h2>
          <InformationButton pagePath="evcp/legal-review" />
          {/* ✅ EvaluationTargetsPage와 동일하게 Badge 추가 */}
          <Badge variant="outline" className="text-sm">
            {currentYear}년
          </Badge>
        </div>
      </div>

      {/* Table */}
      <React.Suspense
        fallback={
          <DataTableSkeleton
            columnCount={13}
            searchableColumnCount={3}
            filterableColumnCount={4}
            cellWidths={[
              "3rem",   // checkbox
              "4rem",   // No.
              "5rem",   // 구분
              "6rem",   // 상태
              "8rem",   // Vendor Code
              "12rem",  // Vendor Name
              "4rem",   // 긴급여부
              "7rem",   // 답변요청일
              "7rem",   // 의뢰일
              "7rem",   // 답변예정일
              "7rem",   // 법무완료일
              "8rem",   // 검토요청자
              "8rem",   // 법무답변자
              "4rem",   // 첨부파일
              "8rem",   // actions
            ]}
            shrinkZero
          />
        }
      >
        {/* ✅ currentYear prop 추가 - EvaluationTargetsTable과 동일한 패턴 */}
        <LegalWorksTable 
          promises={promises} 
          currentYear={currentYear}
        />
      </React.Suspense>
    </Shell>
  );
}